<script>
	import {
		getOpenId
	} from "@/api/login/index.js"
	export default {
		onLaunch: function() {
			//#ifdef MP-WEIXIN || MP-QQ
			const updateManager = uni.getUpdateManager(); // 获取更新管理器对象
			updateManager.onCheckForUpdate(function(res) {
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function() {
								uni.showModal({
									title: '更新提示',
									content: '新版本已经准备好，点击确定重新启动',
									success: res => {
										if (res.confirm) {
											updateManager.applyUpdate();
										}
									}
								})
							}),
							updateManager.onUpdateFailed(function() {
								uni.showModal({
									title: '已经有新版本了哟~',
									content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
								})
							})
					}
				}),
				uni.login({
					success(res) {
						console.log(res)
						getOpenId({
							code: res.code
						}).then(ress => {
							if (ress.code === 0) {
								uni.setStorageSync('openid', ress.data.openid)
								uni.setStorageSync('session_key', ress.data.session_key)
							}
						})
					}
				})
			// #endif
			// app 升级提示
			//#ifdef APP-PLUS
			uni.request({  
				url: 'https://prod-shop-images.oss-cn-beijing.aliyuncs.com/files/frozenApp/output.json',  
				success: (res) => {  
					if (res) {  
					var serverVersion = res.data.version
					 var locationVersionNum = parseInt(plus.runtime.version.replace(/\./g, ""))
					 var serverVersionNum = parseInt(res.data.version.replace(/\./g, ""))
						console.log(locationVersionNum,serverVersionNum)
						if (locationVersionNum < serverVersionNum) {
							uni.showModal({ //提醒用户更新
								title: '发现版本: ' + serverVersion + ',请进行升级',
								content: '建议您在WIFI环境下升级',
								// showCancel:false,
								success: (res) => {  
									if (res.confirm) {
										let appurl=""
										if (plus.os.name=="Android") {  
										    appurl = "https://oss-shop.cciinet.com/files/frozenApp/app-release.apk"; 
										}else{  
										    appurl = "itms-apps://itunes.apple.com/cn/app/%E5%86%BB%E5%93%81%E4%BA%A4%E6%98%93%E6%B8%AF/id1555641756";  
										}
										plus.runtime.openURL(appurl);  
									}  
								}  
							})  
						}
						
					}  
				}  
			})  
			// #endif
		},
		onShow: function() {
		},
		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style>
	@import url('./style/common.css');
	@import url("./font/iconfont.css");
</style>
